﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Documents;

namespace TeamTasker.Client.Presentation.Controls.SearchInTreeControl
{
    public class NodeModel : INotifyPropertyChanged
    {
        private bool _IsVisible;

        private FlowDocument _Label;
        private NodeModel _ParentModel;

        public NodeModel(object data, string plainLabel, int level)
        {
            Data = data;
            SubModels = new ObservableCollection<NodeModel>();
            Label = new FlowDocument(new Paragraph(new Run(plainLabel)));
            Level = level;
            IsVisible = true;
        }

        public bool IsVisible
        {
            get { return _IsVisible; }
            set
            {
                // ReSharper disable RedundantCheckBeforeAssignment
                if (_IsVisible != value)
                    // ReSharper restore RedundantCheckBeforeAssignment
                {
                    _IsVisible = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("IsVisible"));
                    }
                }
            }
        }

        public NodeModel ParentModel
        {
            get { return _ParentModel; }
            set
            {
                if (_ParentModel != value)
                {
                    _ParentModel = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("ParentModel"));
                    }
                }
            }
        }

        public FlowDocument Label
        {
            get { return _Label; }
            set
            {
                if (_Label != value)
                {
                    _Label = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Label"));
                    }
                }
            }
        }

        public string PlainLabel
        {
            get
            {
                string plainText = "";
                foreach (Block block in _Label.Blocks)
                {
                    if (block is Paragraph)
                    {
                        foreach (Inline inline in (block as Paragraph).Inlines)
                        {
                            plainText += new TextRange(inline.ContentStart, inline.ContentEnd).Text;
                        }
                    }
                }
                return plainText;
            }
        }

        public int Level { get; private set; }

        public ObservableCollection<NodeModel> SubModels { get; private set; }

        public object Data { get; private set; }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}